﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;


public partial class Main_Accounts_AccountingSettings : System.Web.UI.Page
{
    string strsql = string.Empty;
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            _BindSettings();
        }
    }
    private void _BindSettings()
    {
        DataTable DT = DU.getdatausingprocedure("USP_ACC_GetAllAccountSettings");
        if (DT.Rows.Count > 0)
        {
            rbtLargeAmount.SelectedValue = Convert.ToDouble(DT.Rows[0]["CHECKSTATUS"]).ToString();//SETTINGID 1
            rbtNagetive.SelectedValue = Convert.ToDouble(DT.Rows[1]["CHECKSTATUS"]).ToString();//SETTINGID 2
            ddlDateFormat.SelectedValue = DT.Rows[2]["SETTINGVALUE"].ToString();//SETTINGID 3
            ddlDecimalPlaces.SelectedValue = DT.Rows[3]["SETTINGVALUE"].ToString();//SETTINGID 4
            ddlCurrencyName.SelectedValue = DT.Rows[4]["SETTINGVALUE"].ToString();//SETTINGID 5
            ddlCurrencyDecimal.SelectedValue = DT.Rows[5]["SETTINGVALUE"].ToString();//SETTINGID 6
            ddlCurrencySymbol.SelectedValue = DT.Rows[6]["SETTINGVALUE"].ToString();//SETTINGID 7
            chkAllowSubLedger.Checked = Convert.ToBoolean(DT.Rows[14]["CHECKSTATUS"]);//SETTINGID 15
            chkEnableTDS.Checked = Convert.ToBoolean(DT.Rows[27]["CHECKSTATUS"]);//SETTINGID 28
            ddlTdsRoundOff.SelectedValue = DT.Rows[28]["SETTINGVALUE"].ToString();//SETTINGID 29
            chkAvailST.Checked = Convert.ToBoolean(DT.Rows[29]["CHECKSTATUS"]);//SETTINGID 30
            txtSTCredit.Text = DT.Rows[29]["SETTINGVALUE"].ToString();//SETTINGID 30
            chkCostCentre.Checked = Convert.ToBoolean(DT.Rows[30]["CHECKSTATUS"]);//SETTINGID 31
        }
    }
    private void _UpdateAccountSettings()
    {
        SqlParameter[] sparam =//Update display large amount
                                {
                                    new SqlParameter("@SETTINGID","1"),
                                    new SqlParameter("@CHECKSTATUS",rbtLargeAmount.SelectedValue)                                    
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam);
        SqlParameter[] sparam1 =//Update negative value format
                                {
                                    new SqlParameter("@SETTINGID","2"),
                                    new SqlParameter("@CHECKSTATUS",rbtNagetive.SelectedValue)                                   
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam1);
        SqlParameter[] sparam2 =//Update date format
                                {
                                    new SqlParameter("@SETTINGID","3"),
                                    new SqlParameter("@CHECKSTATUS","1"),
                                    new SqlParameter("@SETTINGVALUE",ddlDateFormat.SelectedValue)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam2);
        SqlParameter[] sparam3 =//Update decimal place after amount
                                {
                                    new SqlParameter("@SETTINGID","4"),
                                    new SqlParameter("@CHECKSTATUS","1"),
                                    new SqlParameter("@SETTINGVALUE",ddlDecimalPlaces.SelectedValue)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam3);
        SqlParameter[] sparam4 =//Update  currency name
                                {
                                    new SqlParameter("@SETTINGID","5"),
                                    new SqlParameter("@CHECKSTATUS","1"),
                                    new SqlParameter("@SETTINGVALUE",ddlCurrencyName.SelectedValue)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam4);
        SqlParameter[] sparam5 =//Update  currency decimal type like paise, cents, pence.
                                {
                                    new SqlParameter("@SETTINGID","6"),
                                    new SqlParameter("@CHECKSTATUS","1"),
                                    new SqlParameter("@SETTINGVALUE",ddlCurrencyDecimal.SelectedValue)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam5);
        SqlParameter[] sparam6 =//Update  currency symbol
                                {
                                    new SqlParameter("@SETTINGID","7"),
                                    new SqlParameter("@CHECKSTATUS","1"),
                                    new SqlParameter("@SETTINGVALUE",ddlCurrencySymbol.SelectedValue)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam6);
        SqlParameter[] sparam7 =//Update allow sub ledger under account ledger
                                {
                                    new SqlParameter("@SETTINGID","15"),
                                    new SqlParameter("@CHECKSTATUS",chkAllowSubLedger.Checked)                                   
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam7);
        SqlParameter[] sparam8 =//Update  enable tds
                                {
                                    new SqlParameter("@SETTINGID","28"),
                                    new SqlParameter("@CHECKSTATUS",chkEnableTDS.Checked)                                 
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam8);
        SqlParameter[] sparam9 =//Update TDS round off
                                {
                                    new SqlParameter("@SETTINGID","29"),
                                    new SqlParameter("@CHECKSTATUS","0"),
                                    new SqlParameter("@SETTINGVALUE",ddlTdsRoundOff.SelectedValue)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam9);
        SqlParameter[] sparam10 =//Update service tax avail
                                {
                                    new SqlParameter("@SETTINGID","30"),
                                    new SqlParameter("@CHECKSTATUS",chkAvailST.Checked),
                                    new SqlParameter("@SETTINGVALUE",txtSTCredit.Text)
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam10);

        SqlParameter[] sparam11 =//Update Cost Center
                                {
                                    new SqlParameter("@SETTINGID","31"),
                                    new SqlParameter("@CHECKSTATUS",chkCostCentre.Checked)                                   
                                };
        DU.executeprocedure("USP_ACC_SaveAccountSettings", sparam11);

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                _UpdateAccountSettings();
                scope.Complete();
            }
            _BindSessionSettings();
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Updated Succesfuly..";

        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
    private void _BindSessionSettings()
    {
        DataTable DT = DU.getdatausingprocedure("USP_ACC_GetAllAccountSettings");
        if (DT.Rows.Count > 0)
        {
            Session["LargeAmount"] = Convert.ToDouble(DT.Rows[0]["CHECKSTATUS"]).ToString();//SETTINGID 1
            Session["Negative"] = Convert.ToDouble(DT.Rows[1]["CHECKSTATUS"]).ToString();//SETTINGID 2
            Session["DateFormat"] = DT.Rows[2]["SETTINGVALUE"].ToString();//SETTINGID 3
            Session["DecimalPlace"] = DT.Rows[3]["SETTINGVALUE"].ToString();//SETTINGID 4
            Session["CurrencyName"] = DT.Rows[4]["SETTINGVALUE"].ToString();//SETTINGID 5
            Session["CurrencyDecimal"] = DT.Rows[5]["SETTINGVALUE"].ToString();//SETTINGID 6
            Session["CurrencySymbol"] = DT.Rows[6]["SETTINGVALUE"].ToString();//SETTINGID 7
            Session["AllowSubLedger"] = Convert.ToBoolean(DT.Rows[14]["CHECKSTATUS"]);//SETTINGID 15
            Session["EnableTDS"] = Convert.ToBoolean(DT.Rows[27]["CHECKSTATUS"]);//SETTINGID 28
            Session["TDSRoundOdd"] = DT.Rows[28]["SETTINGVALUE"].ToString();//SETTINGID 29
            Session["AvailST"] = Convert.ToBoolean(DT.Rows[29]["CHECKSTATUS"]);//SETTINGID 30
            Session["STCredit"] = DT.Rows[29]["SETTINGVALUE"].ToString();//SETTINGID 30
            Session["AllowCostCenter"] = Convert.ToBoolean(DT.Rows[30]["CHECKSTATUS"]);//SETTINGID 31
        }
    }
}
